<template>
  <div id="task">
    <c-title :hide="false" text="任务包"></c-title>
    <div class="headBox">
      <div class="memberBox">
        <div class="userimg">
          <img :src="info.avatar" alt="" />
        </div>
        <div class="memberinfo">
          <div class="name">Hi，{{ info.nickname }}</div>
          <div class="my">
            <div class="myright" @click="toMy">
              <img src="@/assets/images/taskPackage/user@2x.png" alt="" />
              我的
            </div>
          </div>
          <div class="grade">{{ info.level_name ? info.level_name : "" }}</div>
        </div>
      </div>
      <div class="detailBox">
        <div class="detail left_detail">
          <div class="num">{{ info.total_amount || 0 }}</div>
          <div class="txt">总释放</div>
        </div>
        <div class="detail" @click="toRecord">
          <div class="num">{{ info.receive_reward || 0 }}</div>
          <div class="txt">已获得</div>
          <van-icon name="arrow" class="detail_arrow" />
        </div>
      </div>
    </div>
    <div class="contenBox">
      <div class="taskBox">
        <div class="title">
          <span>完成任务获得{{ usable_name }}</span>
          <div class="toTask" v-if="can_recovery == 1" @click="makeTask">做任务</div>
          <div class="toTask disable" v-if="can_recovery == 3 || !info.status">做任务</div>
          <div class="toTask" v-if="can_recovery == 2" @click="payTask">复活</div>
        </div>
        <div class="tasklineBox">
          <template v-if="info.status">
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in videoArr" :key="index">
                <div class="status" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, videoArr)">
                  <div class="img">
                    <img src="@/assets/images/taskPackage/video@2x.png" v-if="item.status" alt="" />
                    <img src="@/assets/images/taskPackage/video1@2x.png" v-if="!item.status" alt="" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name">{{ item.name }}</div>
              </div>
            </div>
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in shareVideoArr" :key="index">
                <div class="status_pyq" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, shareVideoArr)">
                  <div class="imgpyq">
                    <img src="@/assets/images/taskPackage/pyq@2x.png" v-if="item.status" alt="" />
                    <img src="@/assets/images/taskPackage/pyq1@2x.png" v-if="!item.status" alt="" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name_pyq">{{ item.name }}</div>
              </div>
            </div>
            <div class="videoList">
              <div class="videoLis" v-for="(item, index) in article" :key="index">
                <div class="status_share" v-if="item.status">已完成</div>
                <div class="status_no" v-if="!item.status">未完成</div>
                <div class="line" :class="checkBackground(index, item, article)">
                  <div class="imgShare">
                    <img src="@/assets/images/taskPackage/fx@2x.png" alt="" v-if="item.status" />
                    <img src="@/assets/images/taskPackage/fx1@2x.png" alt="" v-if="!item.status" />
                  </div>
                </div>
                <div :class="!item.status ? 'name_default' : ''" class="name_share">{{ item.name }}</div>
              </div>
            </div>
          </template>
          <template v-if="!info.status">
            <div class="isNone">暂无任务~</div>
          </template>
          <template v-if="info.active_end">
            <van-count-down :time="this.timeCountDown()" format="DD 天 HH 时 mm 分 ss 秒">
              <template #default="timeData">
                <span class="colon">距离任务结束还剩{{ timeData.days }}天</span>
                <span class="block">{{ timeData.hours < 10 ? `0${timeData.hours}` : `${timeData.hours}` }}</span>
                <span class="colon">:</span>
                <span class="block">{{ timeData.minutes < 10 ? `0${timeData.minutes}` : `${timeData.minutes}` }}</span>
                <span class="colon">:</span>
                <span class="block">{{ timeData.seconds < 10 ? `0${timeData.seconds}` : `${timeData.seconds}` }}</span>
              </template>
            </van-count-down>
          </template>
        </div>
      </div>
      <date @postChild="childGet" :info="info" :acticeDay="acticeDay" v-if="info && acticeDay"></date>
      <div class="desc" v-if="info.desc">
        <div class="title">任务说明</div>
        <div class="contenDesc" v-html="info.desc"></div>
      </div>
    </div>
    <van-popup v-model="showShare" position="bottom" :style="{ background: 'transparent' }">
      <div class="sharlist">
        <div class="list">
          <div class="lis" v-for="(item, index) in options" :key="index" @click="shareHandle(item)">
            <img :src="item.icon" alt="" />
            <div class="name">{{ item.name }}</div>
          </div>
        </div>
        <div class="cancelBtn" @click="showShare = false">取消</div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import date from "./components/member_date";
export default {
  data() {
    return {
      videoArr: [],
      shareVideoArr: [],
      article: [],
      can_recovery: false, //是否已经复活，true是可以重新做任务，false是需要先去支付复活费用
      showShare: false,
      options: [
        { name: "视频", icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fshipin.png", router: "VideoList" },
        { name: "文章", icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fwenzhang.png", router: "notice" }
      ],
      acticeMonth: "", //当前选择的年月
      acticeDay: "",
      usable_name: "",
      page: 1,
      info: {}
    };
  },
  components: { date },
  activated() {
    this.showShare = false;
    let dateIs = new Date().getMonth() + 1;
    dateIs = dateIs < 10 ? `0${dateIs}` : dateIs;
    this.acticeMonth = new Date().getFullYear() + "-" + dateIs; //默认当前年月
    this.getData();
    this.getDailyData();
  },
  mounted() {},
  methods: {
    timeCountDown(){
      let end = this.formatIosDate(this.info.active_end);
      end +=' 23:59:59' ;
      let down = new Date(end).getTime() - Number(new Date().getTime().toFixed(0));
      return down
    },
    formatIosDate(date) {
      return date.replace(/\-/g, '/')
    },
    toRecord() {
      this.$router.push(this.fun.getUrl("taskPackageRecord", { type: 1, id: this.info.id }));
    },
    makeTask() {
      this.showShare = true;
    },
    async payTask() {
      // 创建复活记录
      let { result, msg, data } = await $http.get("plugin.task-package.frontend.recovery.create", { record_id: this.$route.params.id, day: this.acticeDay }, "");
      if (result == 0) return this.$toast(msg);
      this.payTaskCreat(data.recovery_order_id);
    },
    async payTaskCreat(id) {
      let { result, msg, data } = await $http.get("plugin.task-package.frontend.create.index", { recovery_order_id: id }, "");
      if (result == 0) return this.$toast(msg);
      this.$router.push(this.fun.getUrl("orderpay", { order_ids: data.order_ids }));
    },
    childGet(e) {
      console.log(e);
      this.acticeDay = e.date;
      this.getDailyData(e.date);
    },
    shareHandle(item) {
      this.$router.push(this.fun.getUrl(item.router, {}, { taskID: this.$route.params.id, taskDay: this.acticeDay }));
    },
    toMy() {
      this.$router.push(this.fun.getUrl("taskPackageMy", {}));
    },
    checkBackground(index, item, arr) {
      if (item.status) {
        if (item.last) {
          if (index == arr.length - 1) {
            return "lineSelect_all";
          }
          return "lineSelect";
        } else {
          return "lineSelect_all";
        }
      }
    },
    getData() {
      let url = "plugin.task-package.frontend.record.info";
      $http.post(url, { id: this.$route.params.id, month: this.acticeMonth }, "加载中..").then(response => {
        if (response.result == 1) {
          this.info = response.data;
        } else {
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    async getDailyData(day) {
      if (!this.acticeDay) {
        let days = new Date().getDate();
        if (this.acticeMonth < 10) {
          this.acticeMonth = `0${this.acticeMonth}`;
        }
        if (days < 10) {
          days = `0${days}`;
        }
        this.acticeDay = day || this.acticeMonth + "-" + days;
      }
      let { result, msg, data } = await $http.get("plugin.task-package.frontend.daily.info", { record_id: this.$route.params.id, day: this.acticeDay }, ".");
      if (result == 0) return this.$toast(msg);
      let { can_recovery, video, share, article, usable_name } = data;
      this.usable_name = usable_name || "可用爱心值";
      this.can_recovery = can_recovery || false;
      this.videoArr = video || [];
      this.shareVideoArr = share || [];
      this.article = article || [];
    }
  }
};
</script>
<style lang="scss" scoped>
#task {
  padding-bottom: 5rem;

  .block,
  .colon {
    font-size: 0.63rem;
    color: #ff8924;
  }

  .sharlist {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #7b7c7d;
    border-radius: 1rem 1rem 0 0;

    .cancelBtn {
      height: 2.81rem;
      background: #fff;
      text-align: center;
      line-height: 2.81rem;
    }

    .list {
      display: flex;
      justify-content: space-around;
      margin-bottom: 0.28rem;
      background: #fff;
      border-radius: 1rem 1rem 0 0;

      .lis {
        padding: 1.71rem 0 1.66rem 0;
        text-align: center;

        img {
          width: 2.72rem;
          height: 2.72rem;
          border-radius: 100%;
        }

        .name {
          font-size: 0.81rem;
          color: #333;
          font-weight: 400;
        }
      }
    }
  }
}

.contenBox {
  width: 100%;
  margin-top: -1.63rem;
  padding: 0 0.62rem;
  box-sizing: border-box;

  .taskBox {
    padding: 1rem 0;
    background: #fff;
    border-radius: 0.31rem;
    font-size: 0.88rem;

    .tasklineBox {
      margin-top: 1.19rem;

      .isNone {
        font-size: 0.75rem;
        color: #9a9a9a;
        text-align: center;
        margin-top: 2.13rem;
      }

      .videoList::-webkit-scrollbar {
        height: 0 !important;
      }

      .videoList:last-child {
        margin-bottom: 0;
      }

      .videoList {
        display: flex;
        overflow-x: scroll;
        margin-bottom: 0.84rem;

        .videoLis:last-child {
          margin-right: 0.87rem;
        }

        .videoLis:first-child {
          margin-left: 0.87rem;
        }

        .videoLis:first-child .line {
          border-radius: 0.19rem 0 0 0.19rem;
        }

        .videoLis:last-child .line {
          border-radius: 0 0.19rem 0.19rem 0;
        }

        .videoLis {
          position: relative;
          flex-shrink: 0;
          width: 20%;
          display: flex;
          flex-direction: column;
          align-items: center;

          .status {
            font-size: 0.63rem;
            font-weight: 400;
            color: #f55e55;
            // line-height: 0.63rem;
          }

          .status_pyq {
            font-size: 0.63rem;
            font-weight: 400;
            color: #07c160;
            // line-height: 0.63rem;
          }

          .status_share {
            font-size: 0.63rem;
            font-weight: 400;
            color: #29abe2;
            // line-height: 0.63rem;
          }

          .status_no {
            font-size: 0.63rem;
            font-weight: 400;
            color: #9a9a9a;
            // line-height: 0.63rem;
          }

          .lineSelect {
            background: linear-gradient(to right, #f55e55 0%, #f55e55 50%, #e6e6e6 51%, #e6e6e6 100%) !important;
          }

          .lineSelect_all {
            background: #f55e55 !important;
          }

          .line {
            width: 100%;
            height: 0.38rem;
            background: #e6e6e6;
            margin: 0.91rem 0 0.97rem 0;
            display: flex;
            align-items: center;
            justify-content: center;

            .imgpyq {
              width: 1.25rem;
              height: 1.25rem;
              border-radius: 100%;

              img {
                width: 1.25rem;
                height: 1.25rem;
                border-radius: 100%;
              }
            }

            .imgShare {
              width: 1.31rem;
              height: 1.28rem;
              background: #fff;

              img {
                width: 1.31rem;
                height: 1.28rem;
              }
            }

            .img {
              width: 1.41rem;
              height: 1.06rem;

              img {
                width: 1.41rem;
                height: 1.06rem;
              }
            }
          }

          .name {
            font-size: 0.69rem;
            font-weight: 400;
            color: #f55e55;
          }

          .name_pyq {
            font-size: 0.69rem;
            font-weight: 400;
            color: #07c160;
          }

          .name_share {
            font-size: 0.69rem;
            font-weight: 400;
            color: #29abe2;
          }

          .name_default {
            color: #000;
          }
        }
      }
    }

    .title {
      display: flex;
      justify-content: space-between;
      padding: 0 0.69rem 0 0.72rem;
      align-items: center;

      span {
        font-size: 0.88rem;
        color: #242424;
      }

      .toTask {
        line-height: 0.66rem;
        padding: 0.41rem 0.78rem;
        background: #ff8924;
        border-radius: 0.75rem;
        font-size: 0.69rem;
        color: #fff;
      }

      .disable {
        background: #bcbcbc;
      }
    }
  }
}

.headBox {
  background: #ff8924;
  padding: 1.38rem 0 2.5rem 0;

  .detailBox {
    width: 15.63rem;
    height: 3.44rem;
    background: #ffffb87c;
    border-radius: 1.72rem;
    margin: 0 auto;
    margin-top: 1.22rem;
    display: flex;
    justify-content: space-between;
    padding: 0.72rem 0 0.78rem 0;

    .left_detail::before {
      content: "";
      position: absolute;
      right: 0;
      top: 0;
      height: 100%;
      width: 1px;
      background: #fff;
    }

    .detail {
      position: relative;
      flex: 1;
      color: #662f00;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .detail_arrow {
        position: absolute;
        right: 1rem;
        top: 0.63rem;
      }

      .num {
        font-size: 0.81rem;
        font-weight: bold;
        line-height: 0.63rem;
      }

      .txt {
        font-size: 0.69rem;
        color: #662f00;
        line-height: 0.66rem;
      }
    }
  }

  .memberBox {
    margin-left: 0.97rem;
    display: flex;

    .userimg {
      width: 3.44rem;
      height: 3.44rem;
      border-radius: 100%;
      margin-right: 0.44rem;

      img {
        width: 3.44rem;
        height: 3.44rem;
        border-radius: 100%;
      }
    }

    .memberinfo {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-start;
      overflow: hidden;

      .grade {
        padding: 0 0.69rem;
        box-sizing: border-box;
        background: #fff;
        border-radius: 0.63rem;
        font-size: 0.69rem;
        color: #ba750c;
        line-height: 1.3rem;
      }

      .myright {
        padding: 0.25rem 0.44rem;
        border-radius: 1rem 0 0 1rem;
        background: #ffb87c;
        color: #fff;
        display: flex;
        align-items: center;
        position: absolute;
        right: 0;
        top: -1rem;
      }

      .my {
        position: relative;
        font-size: 0.69rem;
        font-weight: 400;
        color: #fff;
        width: 100%;

        img {
          width: 0.66rem;
          height: 0.72rem;
          margin-right: 0.22rem;
        }
      }

      .name {
        font-size: 1rem;
        font-weight: bold;
        color: #fff;
        padding-right: 4rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 100%;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}

.desc {
  padding: 0 0.97rem;
  padding-bottom: 0.97rem;
  padding-top: 0.91rem;
  margin-top: 0.63rem;
  overflow: hidden;
  background: #fff;
  border-radius: 0.31rem;

  .title {
    font-size: 0.94rem;
    color: #242424;
    margin-bottom: 1rem;
    text-align: center;
  }

  .contenDesc {
    text-align: left;
  }
}
</style>